﻿using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using iOSClub.App.Desktop.ViewModels.Pages;
using iOSClub.App.Desktop.Views;
using iOSClub.App.Share.Serves;

namespace iOSClub.App.Desktop.Pages;

public partial class SignupPage : UserControl
{
    private int count;

    public SignupPage()
    {
        InitializeComponent();
    }

    protected override async void OnInitialized()
    {
        base.OnInitialized();
        using var userServer = new UserServer();
        var academies = await userServer.GetAcademies();
        if (DataContext is not SignupPageModel model) return;
        foreach (var academy in academies)
            model.Academies.Add(academy);
    }

    public async void Next(object source, RoutedEventArgs args)
    {
        if (count == 2)
        {
            if (DataContext is not SignupPageModel model) return;
            var stu = model.GetStudentModel();
            var view = ViewOpera.GetView<MainWindow>(this);
            if (view == null) return;
            if (stu is null)
            {
                view.NotificationShow("注册失败", "请检查输入信息", NotificationType.Error);
                return;
            }

            if (!await view.SignUp(stu))
            {
                view.NotificationShow("注册失败", "请检查输入信息", NotificationType.Error);
            }

            return;
        }

        Carousel.Next();
        count++;
        if (count == 2)
        {
            NextButton.Content = "完成";
            return;
        }

        if (count == 1)
        {
            PreButton.IsVisible = true;
        }
    }

    public void Previous(object source, RoutedEventArgs args)
    {
        Carousel.Previous();
        count--;
        if (count == 1) NextButton.IsVisible = true;
        if (count == 0) PreButton.IsVisible = false;
    }
}